/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.modules.course.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.Constant;
import io.renren.common.utils.MapUtils;
import io.renren.modules.course.dao.ClassificationDao;
import io.renren.modules.course.dao.CourseDao;
import io.renren.modules.course.dao.CourseMenuDao;
import io.renren.modules.course.dao.DirectionDao;
import io.renren.modules.course.entity.CourseMenuEntity;
import io.renren.modules.course.entity.TreeEntity;
import io.renren.modules.course.service.CourseMenuService;
import io.renren.modules.sys.service.SysRoleMenuService;
import io.renren.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("CourseMenuService")
public class CourseMenuServiceImpl  extends ServiceImpl<CourseMenuDao, CourseMenuEntity> implements CourseMenuService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    DirectionDao directionDao;
    @Autowired
    ClassificationDao classificationDao;
    @Autowired
    CourseDao courseDao;

    @Override
    public List<CourseMenuEntity> queryListParentId(Long parentId, List<Long> menuIdList) {
        List<CourseMenuEntity> menuList = queryListParentId(parentId);
        if(menuIdList == null){
            return menuList;
        }

        List<CourseMenuEntity> userMenuList = new ArrayList<>();
        for(CourseMenuEntity menu : menuList){
            if(menuIdList.contains(menu.getMenuId())){
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<CourseMenuEntity> queryListParentId(Long parentId) {
        return baseMapper.queryListParentId(parentId);
    }

    @Override
    public List<TreeEntity> queryNotButtonList() {

        List<TreeEntity> resultTreeList=new ArrayList();
        List<TreeEntity> directionTree=directionDao.getDirectionTree();
        for(TreeEntity t:directionTree) {
            Long directionId=t.getId();
            List<TreeEntity>  classificationTree=classificationDao.getClassificationTree(directionId);
            for(TreeEntity t1: classificationTree){
                Long classIfcitonId=t1.getId();
                List<TreeEntity> courseTree=courseDao.getCourseTreeByClassIficationId(classIfcitonId);
                for(TreeEntity t2:courseTree) {
                    t2.setParentId(t1.getId()+10000);
                    resultTreeList.add(t2);
                }
                t1.setId(t1.getId()+10000);
                t1.setParentId(t.getId()+1000000);
                resultTreeList.add(t1);
            }
            t.setId(t.getId()+1000000);
            resultTreeList.add(t);
        }

        return resultTreeList;
    }

    @Override
    public List<CourseMenuEntity> getUserMenuList(Long userId) {
        //系统管理员，拥有最高权限
        if(userId == Constant.SUPER_ADMIN){
            return getAllMenuList(null);
        }

        //用户菜单列表
        List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    @Override
    public void delete(Long menuId){
        //删除菜单
        this.deleteById(menuId);
        //删除菜单与角色关联
        sysRoleMenuService.deleteByMap(new MapUtils().put("menu_id", menuId));
    }

    @Override
    public List<TreeEntity> queryCourseList(String searching,Long userId) {
        return courseDao.queryCourseList(searching,userId);
    }

    /**
     * 获取所有菜单列表
     */
    private List<CourseMenuEntity> getAllMenuList(List<Long> menuIdList){
        //查询根菜单列表
        List<CourseMenuEntity> menuList = queryListParentId(0L, menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    /**
     * 递归
     */
    private List<CourseMenuEntity> getMenuTreeList(List<CourseMenuEntity> menuList, List<Long> menuIdList){
        List<CourseMenuEntity> subMenuList = new ArrayList<CourseMenuEntity>();

        for(CourseMenuEntity entity : menuList){
            //目录
            if(entity.getType() == Constant.MenuType.CATALOG.getValue()){
                entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }
}
